//
//  Ruler.swift
//  WatermarkCamera
//
//  Created by AxcLogo on 4/7/2024.
//

import AxcBedrock

// 状态栏高度

var StatusBarHeight: CGFloat {
    if #available(iOS 13.0, *),

       let statusManager = AxcApp.KeyWindow?.windowScene?.statusBarManager {
        return statusManager.statusBarFrame.size.height

    } else {
        return UIApplication.shared.statusBarFrame.size.height
    }
}

// 内容边距

let StandardEdge: UIEdgeInsets = .init(top: 12, left: 16, bottom: 12, right: 16)

// 屏幕宽度

var ScreenWidth: CGFloat {
    return ScreenSize.width
}

// 屏幕高度

var ScreenHeight: CGFloat {
    return ScreenSize.height
}

// 屏幕大小

var ScreenSize: CGSize {
    return Screen.size
}

// 屏幕坐标

var Screen: CGRect {
    return UIScreen.main.bounds
}

// 参考宽度

let StandardWidth: CGFloat = 375

// 参考高度

let StandardHeight: CGFloat = 812

/// 顶部安全区
var SafeAreaTop: CGFloat {
    return SafeAreaInsets.top
}

/// 左侧安全区
var SafeAreaLeft: CGFloat {
    return SafeAreaInsets.left
}

/// 底部安全区
var SafeAreaBottom: CGFloat {
    return SafeAreaInsets.bottom
}

/// 右侧安全区
var SafeAreaRight: CGFloat {
    return SafeAreaInsets.right
}

/// 安全区边距
var SafeAreaInsets: UIEdgeInsets {
    if #available(iOS 11.0, *) {
        if let inset = AxcApp.KeyWindow?.rootViewController?.view.safeAreaInsets {
            return inset
        }
    }
    return .zero
}

extension AxcUnifiedNumber {
    // 转换成自适应Font

    var cmera_font: UIFont {
        let fontSize: CGFloat = .Axc.Create(self)

        return fontSize.axc.uiFont
    }

    // 计算与屏幕水平响应的适配值

    var cmera_autoHor: CGFloat {
        return cmera_ratioHor * ScreenWidth
    }

    // 计算与屏幕垂直响应的适配值

    var cmera_autoVer: CGFloat {
        return cmera_ratioVer * ScreenHeight
    }

    // 计算与屏幕水平比率

    var cmera_ratioHor: CGFloat {
        return cmera_ratio(StandardWidth)
    }

    // 计算与屏幕垂直比率

    var cmera_ratioVer: CGFloat {
        return cmera_ratio(StandardHeight)
    }

    // 计算与某个参考系的比率

    // - Parameter reference: 参考系值

    // - Returns: CGFloat

    func cmera_ratio(_ reference: AxcUnifiedNumber) -> CGFloat {
        let ra = CGFloat.Axc.Create(self) / CGFloat.Axc.Create(reference)

        return ra
    }
}

extension AxcUnifiedNumber {
    // 转换为时间字符串

    var cmera_secondsToTime: String {
        let time = Double.Axc.Create(self)

        guard time >= 0 else { return "-/-" }

        var seconds: Double = 0

        var minutes: Double = 0

        if time > 60 {
            minutes = time / 60

            seconds = time - floor(minutes) * 60

        } else {
            seconds = time
        }

        let _seconds: Int = .init(seconds)

        let _minutes: Int = .init(minutes)

        // asd

        let string = String(format: "%02d:%02d", _minutes, _seconds)

        return string
    }
}

/// 统一弹性动画
func SpringAnimation(withDuration: CGFloat = 0.4,
                     block: @escaping AxcBlock.Empty,
                     completion: AxcBlock.Empty? = nil) {
    UIView.animate(withDuration: withDuration,
                   delay: 0,
                   usingSpringWithDamping: 0.6,
                   initialSpringVelocity: 0.01,
                   options: .curveEaseInOut) {
        block()
    } completion: { _ in
        completion?()
    }
}

func ViewSpringAnimation(_ view: UIView,
                         isShow: Bool,
                         block: AxcBlock.Empty? = nil,
                         completion: AxcBlock.Empty? = nil) {
    if isShow { // 隐藏，需要显示
        guard view.isHidden else { return }
        view.isHidden = false
        view.transform = CGAffineTransform.Axc.CreateScale(0)
        view.alpha = 0
        SpringAnimation {
            view.transform = CGAffineTransform.Axc.CreateScale(1)
            view.alpha = 1
            block?()
        } completion: {
            completion?()
        }
    } else { // 显示，需要隐藏
        guard !view.isHidden else { return }
        view.alpha = 1
        SpringAnimation {
            view.alpha = 0
            block?()
        } completion: {
            view.isHidden = true
            completion?()
        }
    }
}
